package com.sharedlease.controller;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.sharedlease.entity.R;
import com.sharedlease.entity.Share_order;
import com.sharedlease.service.Share_orderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Time;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 吴钟富
 * @since 2021-03-22
 */
@RestController
@Api(tags = "共享产品订单相关接口")
@RequestMapping("/share-order")
public class Share_orderController {
    @Autowired
    public Share_orderService share_orderService;

    @GetMapping("/getById/{id}")
    @ApiOperation("根据一个id查询共享产品订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "要查询的共享产品订单id", defaultValue = "1"),

    }
    )
    public Share_order getById(@PathVariable Long id) {
        Share_order share_order = share_orderService.getById(id);
        return share_order;
    }

    @GetMapping("/list")
    @ApiOperation("查询全部共享产品订单")
    public List<Share_order> list() {
        List<Share_order> list = share_orderService.list(null);
        return list;
    }

    @PostMapping("/save")
    @ApiOperation("新增一条共享产品订单")
    public R save(@RequestBody Share_order share_order) {

        return R.operate(share_orderService.save(share_order));
    }

    @PutMapping("/update")
    @ApiOperation("根据id更改一条共享产品订单")
    R update(@RequestBody Share_order share_order) {
        if (share_orderService.updateById(share_order)) {
            return R.ok();
        }
        return R.error();
    }

    @PutMapping("/updateEnd_timeById/{id}")
    @ApiOperation("根据id更改共享产品订单的使用结束时间(结束时间为当前时间)")
    R updateEnd_timeById(@PathVariable Long id) {
        UpdateWrapper<Share_order> wrapper = new UpdateWrapper<>();
        wrapper.eq("id",id);
        Share_order share_order = new Share_order();
        Date date=new Date();
        share_order.setEndTime(date);
        if (share_orderService.update(share_order,wrapper)) {
            return R.ok();
        }
        return R.error();
    }

    @DeleteMapping("/removeById/{id}")
    @ApiOperation("根据id删除一条共享产品订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "要删除的共享产品订单id", defaultValue = "-1"),

    }
    )
    R removeById(@PathVariable Long id) {

        return R.operate(share_orderService.removeById(id));
    }


}

